package com.cwt.admin.manage;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.DateUtils;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.SysNotice;
import com.cwt.service.domain.SysRole;
import com.cwt.service.domain.SysUser;
import com.cwt.service.service.SysNoticeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【】
 * @date 2023年08月29日 17:12:16
 */
@Api(tags = "公告管理", value = "NoticeController")
@RequestMapping("/notice")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("system")
)
public class NoticeController extends BaseController {

    @Autowired
    private SysNoticeService noticeService;

    /**
     * 系统公告分页查询
     *
     * @param info 分页数据信息，及条件信息
     * @return 公告分页数据
     */
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    AjaxResult page(@RequestBody PageInfo info) {
        Page<SysNotice> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        return AjaxResult.success(noticeService.page(page, queryWrapper));
    }

    /**
     * 公告新增
     * @param notice 公告
     * @return 公告新增
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult roleSave(@RequestBody SysNotice notice) {
        notice.setCreateUser(StpUtil.getTokenName());
        notice.setCreateTime(DateUtils.formatFullDateTime(new Date()));
        notice.setPageView(0);
        return AjaxResult.success(noticeService.saveOrUpdate(notice));
    }

    /**
     * 公告文件上传
     * @param file 文件
     * @return 公告文件上传
     */
    @CrossOrigin
    @RequestMapping(value = "/upload", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody AjaxResult upload(@RequestBody MultipartFile file) {
        return noticeService.saveNoticeImg(file);
    }

}
